package com.primb.bench.common.module.repository;

import com.primb.bench.common.module.http.service.ApiClient;
import com.primb.bench.common.module.http.service.ApiService;
import com.primb.bench.project.home.entity.ApplyNextUserEntity;
import com.primb.bench.project.home.entity.ClientInfoEntity;
import com.primb.bench.project.home.entity.EnsureInfoEntity;
import com.primb.bench.project.home.entity.HttpApplyResultEntity;
import com.primb.bench.project.home.entity.HttpResultEntity;
import com.primb.bench.project.home.entity.LoanCalculateInfoEntity;
import com.primb.bench.project.home.entity.LoanInfoEntity;
import com.primb.bench.project.home.entity.LogoInfoEntity;
import com.primb.bench.project.home.entity.PromiseApplyEntity;
import com.primb.bench.project.home.entity.TaxInfoEntity;
import com.primb.bench.project.search.entity.HttpSearchInfoEntity;
import com.primb.bench.project.search.entity.ProcessEntity;
import com.primb.netlibs.converter.RxSchedulerUtils;
import com.primb.netlibs.exception.ApiException;

import java.util.HashMap;
import java.util.List;

import rx.Observable;
import rx.functions.Func1;

/**
 * Created by Chen on 2017/5/24.
 * 功能描述：
 */

public class SearchDetailRepository {


    public static Observable<LoanCalculateInfoEntity> getSearchPriceInfoEntity(String priceId) {
        ApiService apiService = ApiClient.initService(ApiService.class);
        Observable<LoanCalculateInfoEntity> detailEntityObservable = apiService.getSearchPriceInfo(priceId).flatMap(new Func1<HttpSearchInfoEntity, Observable<LoanCalculateInfoEntity>>() {
            @Override
            public Observable<LoanCalculateInfoEntity> call(HttpSearchInfoEntity httpPriceInfoEntity) {
                LoanCalculateInfoEntity infoEntity = new LoanCalculateInfoEntity();
                if (httpPriceInfoEntity.isSuccess()) {
                    HttpSearchInfoEntity.DataBean.DetailInfoBean bean = httpPriceInfoEntity.getData().getDetailInfo();
                    LogoInfoEntity logoInfoEntity = new LogoInfoEntity();
                    logoInfoEntity.setLogoDate(bean.getPriceDate());
                    logoInfoEntity.setLogoManagerName(bean.getCustMngName());
                    logoInfoEntity.setLogoManagerNum(bean.getCustMngId());
                    logoInfoEntity.setLogoNumber(bean.getPriceId());
                    logoInfoEntity.setLogoPart(bean.getOrgDesc());
                    logoInfoEntity.setLogoPartId(bean.getOrg());
                    logoInfoEntity.setLogoProtocolType(bean.getPriceScene());
                    logoInfoEntity.setLogoPriceType(bean.getPriceDesc());
                    logoInfoEntity.setLogoProtocolNum(bean.getAgmId());
                    infoEntity.setLogoInfoEntity(logoInfoEntity);

                    ClientInfoEntity clientInfoEntity = new ClientInfoEntity();
                    clientInfoEntity.setCustomerName(bean.getCustName());
                    clientInfoEntity.setCustomerNum(bean.getCustId());
                    clientInfoEntity.setCustomerType(bean.getCustTyDesc());
                    clientInfoEntity.setCustomerTypeId(bean.getCustType());
                    clientInfoEntity.setCustomerCredit(bean.getCustCvDesc());
                    clientInfoEntity.setCustomerCreditId(bean.getCustCreditLvl());
                    clientInfoEntity.setCustomerSize(bean.getCustSaDesc());
                    clientInfoEntity.setCustomerSizeId(bean.getCustScale());
                    clientInfoEntity.setCustomerContributionNum(bean.getNowRemainGx());
                    clientInfoEntity.setCustomerAdjustRate(bean.getCustPdAdjust());
                    clientInfoEntity.setCustomerIsContribution(bean.getIsScore());
//                      clientInfoEntity.setCustomerNumM(bean.getCustIdM());
//                      clientInfoEntity.setCustomerContributionNumM(bean.getNowRemainGxM());
//                      clientInfoEntity.setCustomerUsedContributionM(bean.getNowUseGxM());
                    clientInfoEntity.setCustomerAllContribution(bean.getNowSumGx());
                    clientInfoEntity.setCustomerUsedContribution(bean.getNowUseGx());
                    clientInfoEntity.setCustomerAllProfit(bean.getNowSumGp());
                    clientInfoEntity.setCustomerInfoEmpty(false);
                    infoEntity.setClientInfoEntity(clientInfoEntity);

                    LoanInfoEntity loanInfoEntity = new LoanInfoEntity();
                    loanInfoEntity.setLoanMoney(bean.getLnAmt());
                    loanInfoEntity.setLoanDate(bean.getTrem());
                    loanInfoEntity.setLoanType(bean.getCurrencyName());
                    loanInfoEntity.setLoanTypeId(bean.getLnCur());
                    loanInfoEntity.setLoanBusinessType(bean.getLnPrdName());
                    loanInfoEntity.setLoanBusinessTypeId(bean.getLnPrd());
                    loanInfoEntity.setLoanIndustry(bean.getCodeName());
                    loanInfoEntity.setLoanIndustryId(bean.getLnIndu());
                    loanInfoEntity.setLoanRateType(bean.getLnIntTypeDesc());
                    loanInfoEntity.setLoanRateTypeId(bean.getLnIntType());
                    loanInfoEntity.setLoanRepaymentId(bean.getRepayPatnCd());
                    loanInfoEntity.setLoanRepayment(bean.getRepayPatnCdDesc());
                    loanInfoEntity.setLoanInfoEmpty(false);
                    infoEntity.setLoanInfoEntity(loanInfoEntity);

                    EnsureInfoEntity ensureInfoEntity = new EnsureInfoEntity();
                    ensureInfoEntity.setEnsureMainTypeId(bean.getLnAssType());
                    ensureInfoEntity.setEnsureMainType(bean.getLnAssTypeType());
                    ensureInfoEntity.setEnsureConcretenessType(bean.getTypeDesc());
                    ensureInfoEntity.setEnsureConcretenessTypeId(bean.getTypeDesc());
                    ensureInfoEntity.setCommonGuarantee(bean.getCommonGuarantee());
                    ensureInfoEntity.setLnOthMortAmt(bean.getLnOthMortAmt());
                    ensureInfoEntity.setLnAcctRecvAmt(bean.getLnAcctRecvAmt());
                    ensureInfoEntity.setHighGuarantee(bean.getHighGuarantee());
                    ensureInfoEntity.setLnPropertyAmt(bean.getLnPropertyAmt());
                    ensureInfoEntity.setLnFinImpawnAmt(bean.getLnFinImpawnAmt());
                    ensureInfoEntity.setEnsureInfoEmpty(false);
                    infoEntity.setEnsureInfoEntity(ensureInfoEntity);

                    TaxInfoEntity taxInfoEntity = new TaxInfoEntity();
                    taxInfoEntity.setTaxVat(bean.getLnVatTax());
                    taxInfoEntity.setTaxIncome(bean.getLnIncomeTax());
                    infoEntity.setTaxInfoEntity(taxInfoEntity);

                    HttpResultEntity.DataBean msgBean = new HttpResultEntity.DataBean();
                    msgBean.setLnpCostBdRate(bean.getLnChangeCostIr());
                    msgBean.setLnpCostRate(bean.getLnCostIr());
                    msgBean.setLnpTargetRate(bean.getCustGdIr());
                    msgBean.setLnpCustRate(bean.getCustGdIr());
                    msgBean.setLnpMarketIr(bean.getLnMarketIr());
                    msgBean.setCapCostRate(bean.getParaCapCostRate());
                    msgBean.setDepFee(bean.getDepFee());
                    msgBean.setDppBdFee(bean.getDppBdFee());
                    msgBean.setElRate(bean.getParaElRate());
                    msgBean.setLncEcPara(bean.getParaCapOcuRate());
                    msgBean.setLncFeeBdRate(bean.getParaPrdChangeFeeRate());
                    msgBean.setLncFtpRate(bean.getParaFtpRate());
                    msgBean.setLncFeeRate(bean.getParaPrdFeeRate());
                    msgBean.setLncLgd(bean.getParaLgdRate());
                    msgBean.setLncLowCapRet(bean.getParaCapRetRate());
                    msgBean.setLncPrdTarRet(bean.getParaPrdTarRet());
                    msgBean.setLncPd(bean.getParaPdRate());
                    msgBean.setLnDiscountValue(bean.getParaScoreRate());
                    msgBean.setLncScoreleft(bean.getParaLeftScoreRate());
                    msgBean.setLncScoreUse(bean.getParaUseScoreRate());
                    infoEntity.setResultInfoEntity(msgBean);

                    PromiseApplyEntity promiseApplyEntity = new PromiseApplyEntity();
//                    try {
//                        double adjustRate = Double.valueOf(bean.getAdjustGdIr()) - Double.valueOf(bean.getCustGdIr());
//                        promiseApplyEntity.setPromiseAdjustRate(adjustRate + "");
//                    } catch (Exception e) {
                    promiseApplyEntity.setPromiseAdjustRate(bean.getPromiseAdjustValue().equals("") ? "0" : bean.getPromiseAdjustValue());
//                    }
                    promiseApplyEntity.setPromiseAdjustDirectRate(bean.getAdjustGdIr().equals("") ? "0" : bean.getAdjustGdIr());
                    if (!bean.getPromBasicAcct().equals("") && !bean.getPromCashDate().equals("")
                            && !bean.getLastDeptAvg().equals("") && !bean.getPromFavoreRate().equals("")
                            && !bean.getPromBeginDate().equals("")) {
                        promiseApplyEntity.setPromiseBasicAccount(!bean.getPromBasicAcct().contains("未"));
                        promiseApplyEntity.setPromiseSalaryAccount(!bean.getPromSalaryAcct().contains("未"));
                        promiseApplyEntity.setPromiseSettleAccount(!bean.getPromForeignAcct().contains("未"));
                        String promBeginDate = bean.getPromBeginDate();
                        promiseApplyEntity.setPromiseStartDate(promBeginDate.substring(0, promBeginDate.indexOf(" ")));
                        String promCashDate = bean.getPromCashDate();

                        promiseApplyEntity.setPromiseEndDate(promCashDate.substring(0, promCashDate.indexOf(" ")));
                        promiseApplyEntity.setPromiseDepositDayNow(bean.getPromDepAvg());
                        promiseApplyEntity.setPromiseDepositDay(bean.getLastDeptAvg());
                        promiseApplyEntity.setPromiseDepositSumNow(bean.getPromDepBal());
                        promiseApplyEntity.setPromiseDepositSum(bean.getLastDeptBal());
                        promiseApplyEntity.setPromiseOtherScene(bean.getPromOtherRate());
                        promiseApplyEntity.setPromiseOtherSceneDetail(bean.getPromOtherScene());
                        promiseApplyEntity.setPromiseRemark(bean.getRemarks());
                        promiseApplyEntity.setPromiseFavorableRate(bean.getPromFavoreRate());
                        promiseApplyEntity.setPromisePoint(bean.getOverdrawScore());
                        promiseApplyEntity.setPromiseAdjustRate(bean.getPromiseAdjustValue());
                        promiseApplyEntity.setPromiseDetail(true);
                    } else {
                        promiseApplyEntity.setPromiseDetail(false);
                    }

                    if (!bean.getApplyRate().equals("") && !bean.getApplyRate().equals("0")) {
                        promiseApplyEntity.setApplyRate(bean.getApplyRate());
                        promiseApplyEntity.setApplyReason(bean.getApplyRateDesc());
                        promiseApplyEntity.setApplyDate(bean.getApplyDate());
                        promiseApplyEntity.setApply(true);
                    } else {
                        promiseApplyEntity.setApply(false);
                    }
                    infoEntity.setPromiseApplyEntity(promiseApplyEntity);

                    infoEntity.setProcessList(httpPriceInfoEntity.getData().getProcess());

                    infoEntity.setBackReason(httpPriceInfoEntity.getData().getDetailInfo().getSendBackReason());
                } else {
                    Observable.error(new ApiException("服务器数据异常，请联系管理员处理"));
                }
                return Observable.just(infoEntity);
            }
        });
        return detailEntityObservable.compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    public static Observable<HttpApplyResultEntity> reapplyRate(HashMap<String, String> map) {
        return ApiClient.initService(ApiService.class).reapplyRate(map).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }


    public static Observable<ApplyNextUserEntity> reapplyNextList(String priceId, String applyRate, String applyReason) {
        HashMap<String, String> map = new HashMap<>();
        map.put("priceId", priceId);
        map.put("applyRate", applyRate);
        map.put("goOrBack", "go");
        map.put("applyLowPriceReason", applyReason);
        return ApiClient.initService(ApiService.class).submitReapplyNextPerson(map).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }
}
